package com.qf.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @version : 1.0
 * @author:宋煜
 * @date: 2022/5/29 16:05
 * 解决非幂等性
 */
@Component
@Slf4j
public class RedisRepeatUtil {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    //跳转到添加页面的固定头
    public static String TOKEN_MEETING_ADD = "token:meeting:meeting-add:";
    //跳转到Excel页面的固定头
    public static String TOKEN_MEETING_EXCEL = "token:meeting:toImportExcel:";
    /**
     * TODO 生成token值 进入添加页面的逻辑
     */
    public String generateToken(String url){
        String uuid = UUID.randomUUID().toString();
        String key = url+uuid;
        redisTemplate.opsForValue().set(key,uuid);
        redisTemplate.expire(key,30, TimeUnit.SECONDS);
        return uuid;
    }

    /**
     *  TODO 进行比较
     * @param key 后台redis key
     * @param uuid 前端穿过来的uuid
     * @return
     */
    public boolean compareToken(String key,String uuid){
        String redisToken = (String) redisTemplate.opsForValue().get(key);
        redisTemplate.delete(key);
        return uuid.equals(redisToken);
    }
}
